function kte = computeKte(i,elements,model)
e = model.getElement(i-1);
face = e.getFace();
xi = e.getGaussPoint(0);
w = e.getGaussWeight(0);
jac = face.jacXAt(xi);
detJac = det(jac);
B = zeros(3,6);
B = e.computeB(face,B,[xi(1) xi(2)]);
C = elements(i).constitutiveMatrix;
ke = w*B'*C*B*abs(detJac);
if elements(i).isCrack==1
    Dhat = [elements(i).Dnn 0;0 elements(i).Dss];
    H = elements(i).H;
    A = elements(i).A;
    if elements(i).isOpening == 1
        gamma = elements(i).gamma;
        if elements(i).isLoading == 1
            t = elements(i).t;
            f = elements(i).f;
%             det(gamma*A*H + Dhat - t*f')
            kte = ke - ke*H*gamma*((Dhat + gamma*A*H - t*f')\A);
        else %unloading
            kte = ke - ke*H*((A*H + Dhat/gamma)\A);
        end
    elseif elements(i).isSticking == 1
        kte = ke;
    elseif elements(i).isSliding == 1
        h2 = H(:,2);
        gamma = elements(i).gamma;
        fs = elements(i).fs;
        s = fs(2);
        e = elements(i).e;
        es = e(2);
        f = elements(i).f;
        Dss = elements(i).Dss;
        if elements(i).isLoading == 1
            %damage grows
            kte = ke*(eye(6) - h2*fs'*A/(s*Dss/gamma + fs'*A*h2 ...
                - s*Dss/gamma^2*es*f));
        else
            %damage does not grow
            kte = ke*(eye(6) - h2*fs'*A/(s*Dss/gamma + fs'*A*h2));
        end
    else
        disp('invalid state');
    end
else
    kte = ke;
end
end
